home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15760 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  103 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: The STL and nested structures
  5. Message-ID: <marnoldDpHB2u.7G2@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4ju9q7$fa1$1@mhadg.production.compuserve.com>
  8. Date: Sun, 7 Apr 1996 06:23:18 GMT
  9. Sender: marnold@netcom19.netcom.com
  10.  
  11. Alan Huff <74312.2300@CompuServe.COM> writes:
  12.  
  13. >I am having a problem using the STL with structures defined within 
  14. >a class definition.  Consider the following code fragment.
  15.  
  16. >>#include <vector.h>
  17. >>class Bar {
  18. >>   struct Foo {
  19. >>      int   value;
  20. >>   };
  21. >>
  22. >>   vector< Foo > fooContainer;
  23. >>}
  24.  
  25. >The compiler I am using (VC 4.1) refuses to compile.  An error is 
  26. >generated the says "'Foo' : undeclared identifier" at line 75 in 
  27. >vector.h.
  28.  
  29. >Line 75 of vector.h
  30. >>   vector(size_type n, const T& value = T()) { ...
  31.  
  32. >If I move the structure Foo out of the class and into the global 
  33. >namespace there are no compilation errors.
  34.  
  35. >What am I missing??
  36.  
  37. A real C++ compiler.  
  38.  
  39. It looks like Visual C++ 4.1 is trying to instantiate vector for
  40. Bar::fooContainer with a type (Bar::Foo) that it hasn't recognized 
  41. yet.  Since the definition of Foo appears before the declaration of
  42. fooContainer in Bar, this shouldn't be happening.  The above code 
  43. should compile without problems (it certainly does with Borland C++).  
  44. Why the latest version of Visual C++ has a different "opinion" about 
  45. this, I can't answer.
  46.  
  47. You can try the following to see if it helps...
  48.  
  49. class Bar {
  50.    struct Foo {
  51.       int   value;
  52.    };
  53.    vector< Bar::Foo > fooContainer;
  54. };
  55.  
  56. ...but explicity qualifying a nested class like this should not be 
  57. required for statements *within* the nesting class' scope.
  58.  
  59. You might even try a forward declaration of Foo to see if that "jogs" 
  60. Visual C++ into realizing Bar::Foo exists at the right time, although
  61. such a forward reference is obviously redundant...
  62.  
  63. class Bar {
  64.    struct Foo {
  65.       int   value;
  66.    };
  67.    struct Foo;
  68.    vector< Foo > fooContainer;
  69. };
  70.  
  71. As a test, you can also replace vector with your own simple template
  72. (that does something similar to vector with the parameterized type) 
  73. and see if you get the same kind of error...
  74.  
  75. template <class T>
  76. struct Test {
  77.    Test(const T& = T()) { }
  78. };
  79.  
  80. class Bar {
  81.    struct Foo {
  82.       int   value;
  83.    };
  84.    Test< Foo > fooTest;
  85. };
  86.  
  87. If so, you'll know the problem has nothing to do with STL, but rather
  88. the general template implementation of your compiler.
  89.  
  90. If none of this leads you to any kind of solution, I guess you'll be 
  91. forced to avoid using nested classes this way with Dismal, uh, Visual 
  92. C++, or you could switch to a compiler which can deal with the above
  93. code.  If you like, you could also complain to Microsoft.
  94.  
  95. Regards,
  96. -------------------------------------------------------------------------
  97. Matt Arnold                       |        | ||| | |||| |  | | || ||
  98. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  99. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  100. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  101. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  102. -------------------------------------------------------------------------
  103.